#ifndef _BSP_USART_H_
#define _BSP_USART_H_

#include <rtconfig.h>
#include <stm32f4xx_hal.h>

/* debug uart begin */

#define DEBUG_UART USART1
#define DEBUG_UART_PIN_GROUP GPIOA
#define DEBUG_UART_TX_PIN GPIO_PIN_9
#define DEBUG_UART_RX_PIN GPIO_PIN_10
#define DEBUG_UART_TX_RX_PIN_AF GPIO_AF7_USART1
#define DEBUG_UART_GPIO_RCC_EN __HAL_RCC_GPIOA_CLK_ENABLE()
#define DEBUG_UART_RCC_EN __HAL_RCC_USART1_CLK_ENABLE()

// #define DEBUG_UART_DMA DMA2
#define DEBUG_UART_TX_DMA_STREAM DMA2_Stream7
#define DEBUG_UART_TX_DMA_CHANNEL DMA_CHANNEL_4

#define DEBUG_UART_TX_BUFFER_SIZE 256
#define DEBUG_UART_RX_BUFFER_SIZE 256

/* Definition for USARTx's NVIC */
#define DEBUG_UART_DMA_TX_IRQn DMA2_Stream7_IRQn
#define DEBUG_UART_DMA_TX_IRQHandler DMA2_Stream7_IRQHandler

#define DEBUG_UART_DMA_CLK_ENABLE __HAL_RCC_DMA2_CLK_ENABLE()
#define DEBUG_UART_IRQn USART1_IRQn
#define DEBUG_UART_HANDLER USART1_IRQHandler

#define DEBUG_UART_FORCE_RESET() __HAL_RCC_USART1_FORCE_RESET()
#define DEBUG_UART_RELEASE_RESET() __HAL_RCC_USART1_RELEASE_RESET()

int bsp_debug_uart_tx(uint8_t *tx_buf, uint16_t tx_size);

int bsp_debug_uart_init(void);
/* debug uart end */

/* subs uart begin */
#define SBUS_UART UART5
#define SBUS_UART_PIN_GROUP GPIOD
#define SBUS_UART_RX_PIN GPIO_PIN_2
#define SBUS_UART_RX_PIN_AF GPIO_AF8_UART5
#define SBUS_UART_GPIO_RCC_EN __HAL_RCC_GPIOD_CLK_ENABLE()
#define SBUS_UART_RCC_EN __HAL_RCC_UART5_CLK_ENABLE()
#define SBUS_DMA_RCC_EN __HAL_RCC_DMA1_CLK_ENABLE()

#define SBUS_DMA DMA1
#define SBUS_UART_RX_DMA_STREAM DMA1_Stream0
#define SBUS_UART_RX_DMA_CHANNEL DMA_CHANNEL_4

#define SBUS_UART_RX_BUFFER_SIZE 0x40

#define SBUS_UART_DMA_RX_IRQn DMA1_Stream0_IRQn
#define SBUS_UART_DMA_RX_IRQHandler DMA1_Stream0_IRQHandler

#define SBUS_UART_DMA_CLK_ENABLE __HAL_RCC_DMA1_CLK_ENABLE()
#define SBUS_UART_IRQn UART5_IRQn
#define SBUS_UART_HANDLER UART5_IRQHandler

#define SBUS_UART_FORCE_RESET() __HAL_RCC_UART5_FORCE_RESET()
#define SBUS_UART_RELEASE_RESET() __HAL_RCC_UART5_RELEASE_RESET()

// 100k波特率，8位数据位，2位停止位，偶校验（EVEN)，无控流，25个字节。
#define SBUS_BAUDURATE 100000
#define SBUS_WORDLENGTH UART_WORDLENGTH_9B
#define SBUS_STOPBIT UART_STOPBITS_1
#define SBUS_PARITY UART_PARITY_EVEN

/// @brief 板上SBUS串口初始化
/// @param
/// @return
int bsp_sbus_uart_init(void);
/* subs uart end */

/* telegram uart begin */
#define TELEGRAM_UART UART4
#define TELEGRAM_UART_PIN_GROUP GPIOA
#define TELEGRAM_UART_TX_PIN GPIO_PIN_0
#define TELEGRAM_UART_RX_PIN GPIO_PIN_1
#define TELEGRAM_UART_PIN_AF GPIO_AF8_UART4
#define TELEGRAM_UART_GPIO_RCC_EN __HAL_RCC_GPIOA_CLK_ENABLE()
#define TELEGRAM_UART_RCC_EN __HAL_RCC_UART4_CLK_ENABLE()
#define TELEGRAM_DMA_RCC_EN __HAL_RCC_DMA1_CLK_ENABLE()

#define TELEGRAM_DMA DMA1
#define TELEGRAM_UART_RX_DMA_STREAM DMA1_Stream2
#define TELEGRAM_UART_RX_DMA_CHANNEL DMA_CHANNEL_4
#define TELEGRAM_UART_TX_DMA_STREAM DMA1_Stream4
#define TELEGRAM_UART_TX_DMA_CHANNEL DMA_CHANNEL_4

#define TELEGRAM_UART_RX_BUFFER_SIZE 0x60
#define TELEGRAM_UART_TX_BUFFER_SIZE 0x60

#define TELEGRAM_UART_DMA_RX_IRQn DMA1_Stream2_IRQn
#define TELEGRAM_UART_DMA_RX_IRQHandler DMA1_Stream2_IRQHandler
#define TELEGRAM_UART_DMA_TX_IRQn DMA1_Stream4_IRQn
#define TELEGRAM_UART_DMA_TX_IRQHandler DMA1_Stream4_IRQHandler

#define TELEGRAM_UART_DMA_CLK_ENABLE __HAL_RCC_DMA1_CLK_ENABLE()
#define TELEGRAM_UART_IRQn UART4_IRQn
#define TELEGRAM_UART_HANDLER UART4_IRQHandler

#define TELEGRAM_UART_FORCE_RESET() __HAL_RCC_UART4_FORCE_RESET()
#define TELEGRAM_UART_RELEASE_RESET() __HAL_RCC_UART4_RELEASE_RESET()

int bsp_telegram_uart_tx(uint8_t *tx_buf, uint16_t tx_size);
int bsp_telegram_uart_init(void);

/* subs uart end */

/* gps uart begin */
#define GPS_UART USART3
#define GPS_UART_PIN_GROUP GPIOD
#define GPS_UART_TX_PIN GPIO_PIN_8
#define GPS_UART_RX_PIN GPIO_PIN_9
#define GPS_UART_PIN_AF GPIO_AF7_USART3
#define GPS_UART_GPIO_RCC_EN __HAL_RCC_GPIOD_CLK_ENABLE()
#define GPS_UART_RCC_EN __HAL_RCC_USART3_CLK_ENABLE()

#define GPS_UART_RX_BUFFER_SIZE 0x60
#define GPS_UART_TX_BUFFER_SIZE 0x60

#define GPS_UART_IRQn USART3_IRQn
#define GPS_UART_HANDLER USART3_IRQHandler

#define GPS_UART_FORCE_RESET() __HAL_RCC_USART3_FORCE_RESET()
#define GPS_UART_RELEASE_RESET() __HAL_RCC_USART3_RELEASE_RESET()

int bsp_gps_uart_init(void);
UART_HandleTypeDef *bsp_gps_handle();
/* subs uart end */

#endif
